'use strict';
const await = require('await-stream-ready/lib/await');
const Controller = require('egg').Controller;
const pageLimit=10;
class PostController extends Controller {
    async sendPost() {
        const { ctx,app } = this;
        ctx.validate({
            user_id:{
                type:'int',
                required: true,
                range: {
                    min: 1,
                    max: 10000
                },
                desc: '发布人ID'
            },
            title:{
                type: 'string',
                required: true,
                range: {
                    min: 1,
                    max: 60
                },
                desc: '帖子标题'
            },
            content:{
                type: 'string',
                required: true,
                range: {
                    min: 1,
                    max: 800
                },
                desc: '帖子文字内容'
            },
            post_type:{
                type: 'int',
                required: true,
                range: {
                    min: 0,
                    max: 3
                },
                desc: '帖子类型'
            },
            topic:{
                type: 'int',
                required: true,
                range: {
                    min: 0,
                    max: 3
                },
                desc: '帖子话题类型'
            },
            price:{
                type: 'int',
                required: true,
                range: {
                    min: 0,
                    max: 1000
                },
                desc: '云养价格'
            },
            titleImg:{
                type: 'string',
                required: true,
                range: {
                    min: 0,
                    max: 1000
                },
                desc: '图片'
            },
            imgList:{
                type: 'string',
                required: true,
                range: {
                    min: 0,
                    max: 1000
                },
                desc: '图片列表'
            }
        })
        let {user_id,title,content,post_type,topic,price,titleImg,imgList} = ctx.request.body
        let post = await app.model.Post.create({
            user_id,
            title,
            content,
            post_type,
            topic,
            price,
            titleImg,
            imgList
        })
        ctx.apiSuccess(post)
    }

    //发送帖子图片
    async sendPostImageList() {
        const { ctx,app } = this;
        
        
        ctx.validate({
            postId:{
                type:'int',
                required: true,
                range: {
                    min: 1,
                    max: 10000
                },
                desc: '帖子ID'
            },
            imagelist:{
                type: 'string',
                required: true,
                range: {
                    min: 1,
                    max: 10000
                },
                desc: '图片URL列表'
            }
        });
        let {postId, imagelist} = ctx.request.body;
        let postImage;
        JSON.parse(imagelist).forEach(element => {
            this.sendPostImage(postId,element);
        });
        
        ctx.apiSuccess(postImage)
    }

    //上传帖子单张图像
    async sendPostImage(post_id,img_url) {
        const { ctx,app } = this;
        let postImage = app.model.PostImage.create({
            postId:post_id,
            imgUrl:img_url
        });
        ctx.apiSuccess(postImage);
    }

    //根据帖子类型查看帖子123
    async typePost() {
        const { ctx,app } = this;
        let page = ctx.params.page ? parseInt(ctx.params.page) : 1;
        let limit=pageLimit;
        let offset=(page-1)*limit;
        let type = ctx.params.type;
        let result;
        result=await app.model.Post.findAll({
            where: {
                post_type:type
            },
            include: [{
                model: app.model.User,
                as:'pos',
                attributes: ['username', 'avatar']
            }],
            offset: offset,
            limit: limit
        })
        ctx.apiSuccess(result)
    }

    //添加图片列表
    async sendList() {
        const {ctx,app} = this;
        ctx.validate({
            postId:{
                type: 'int',
                required: true,
                range: {
                    min: 1,
                    max: 10000
                },
                desc: '帖子ID'
            },
            imagelist:{
                type: 'string',
                required: true,
                range: {
                    min: 0,
                    max: 1000
                },
                desc: '图片'
            }
        })
        let {postId,imagelist} = ctx.request.body;
        let postImage = await app.model.PostImage.create({
            postId:postId,
            imgUrl:imagelist
        })
        ctx.apiSuccess(postImage)
    }

    //添加浏览记录
    async addHistory() {
        const { ctx,app } = this;
        ctx.validate({
            userId:{
                type:'int',
                required: true,
                range: {
                    min: 1,
                    max: 10000
                },
                desc: '浏览用户ID'
            },
            postId:{
                type: 'int',
                required: true,
                range: {
                    min: 1,
                    max: 10000
                },
                desc: '帖子ID'
            }
        })
        let {userId,postId} = ctx.request.body
        let postHistory = await app.model.PostHistory.create({
            userId,
            postId
        })
        ctx.apiSuccess(postHistory)
    }

    //查找帖子
    async searchPost() {
        const { ctx,app } = this;

        let title=ctx.params.title;

        let Op=this.app.Sequelize.Op;
        let data=await app.model.Post.findAll({
            where:{
                title:{
                    [Op.like]: '%'+title+'%'
                }
            },
        });
        ctx.apiSuccess(data);
    }


    //根据id查找一个帖子的具体信息
    async lookPost() {
        const { ctx,app } = this;
        let id=ctx.params.id;
        let data=await app.model.Post.findOne({
            where:{
                id:id
            },
            include: {
                model: app.model.PostImage,
                as:'pim',
            }
        });
        ctx.apiSuccess(data);
    }
}
module.exports = PostController;